/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package org.amarena2d.examples.scenes;

import com.android1.amarena2d.actions.base.Sequence;
import com.android1.amarena2d.actions.interval.DelayTime;
import com.android1.amarena2d.actions.movement.Shake;
import com.android1.amarena2d.animation.Animations;
import com.android1.amarena2d.animation.AtlasAnimation;
import com.android1.amarena2d.commons.Callback;
import com.android1.amarena2d.input.TouchListenerAdapter;
import com.android1.amarena2d.nodes.FpsLabel;
import com.android1.amarena2d.nodes.Scene;
import com.android1.amarena2d.nodes.SimpleLabel;
import com.android1.amarena2d.nodes.behavior.ActionTarget;
import com.android1.amarena2d.nodes.sprites.AtlasBatchedSprite;
import com.android1.amarena2d.nodes.sprites.AtlasSpriteBatchNode;
import com.android1.amarena2d.nodes.sprites.SharedSprite;
import com.android1.amarena2d.texture.Textures;
import com.android1.amarena2d.texture.TiledTextureAtlas;
import com.badlogic.gdx.graphics.Color;
import org.amarena2d.examples.Example;

@Example(name = "TiledTextureAtlas Animation", category = "Texture Atlas")
public class TiledTextureAtlasAnimationExampleScene extends Scene {


    public TiledTextureAtlasAnimationExampleScene() {

        engine.getRenderer().setClearColor(Color.WHITE);

        add(new FpsLabel(10, 20, Color.BLACK));

        //TextureAtlas with 2x4 tiles.
        final TiledTextureAtlas mudAtlas = new TiledTextureAtlas(Textures.$("anim/mud.png").get(), 4, 4);
        //TextureAtlas with 2x2 tiles.
        final TiledTextureAtlas batAtlas = new TiledTextureAtlas(Textures.$("anim/bat_fly.png").get(), 2, 2);


        //we just select the needed tiles by id.
        final AtlasAnimation mudWalkAnimation = Animations.$(mudAtlas, 0.2F).name("MudWalk").addFrames(0, 1, 2, 3, 4, 5, 3).create();
        final AtlasAnimation mudAttackAnimation = Animations.$(mudAtlas, 0.2F).name("MudAttack").addFrames(0, 6, 7, 8, 9, 10, 11).create();

        //By default, we will use all tiles in the atlas.
        final AtlasAnimation batFlyAnimation = Animations.$(batAtlas, 0.2F).create();


        //An animation can be set in the constructor as the default animation.
        //You should run animations on a SharedSprite or BatchedSprite.
        //StaticSprite and Sprite are a bad choice for this as they have their own TexturedMesh that would be updated on each Frame.
        //A SharedSprite will use the AtlasMesh in the TextureAtlas.
        final SharedSprite mud = new SharedSprite(mudWalkAnimation, 200, 100);
        //starts the default animation
        mud.animation().selectDefault().repeatForever().play();

        mud.touch().makeTouchable(new TouchListenerAdapter() {
            @Override
            public boolean onTouchDown(float x, float y, int pointer, boolean isHit) {
                if (!mud.animation().isRunning(mudAttackAnimation)) {
                    mud.animation().select(mudAttackAnimation).onFinished(attackfinished).play();
                    engine.getCamera().action().run(Sequence.$(DelayTime.$(0.7F), Shake.$Y(6, 1F)));
                }
                return true;
            }
        });

        //If you need to run a lot of Sprites with the same animation you should use the SpriteBatch.
        //The AtlasBatchedSprite is a special kind of a BatchedSprite that can only use AtlasTextureFrames from the TextureAtlas given in the constructor.

        AtlasSpriteBatchNode atlasSpriteBatchNode = new AtlasSpriteBatchNode(batFlyAnimation);

        AtlasBatchedSprite bat1 = atlasSpriteBatchNode.createSpriteBatchEntity().xy(50, 180);

        AtlasBatchedSprite bat2 = atlasSpriteBatchNode.createSpriteBatchEntity().xy(150, 200);

        AtlasBatchedSprite bat3 = atlasSpriteBatchNode.createSpriteBatchEntity().xy(350, 190);

        //You could run an explicite animation on each BatchedSprite, but running an animation on the node is more efficient.
        //It will propagate each TextureFrame to his nodes.
        atlasSpriteBatchNode.animation().selectDefault().repeatForever().play();


        add(mud, atlasSpriteBatchNode);

        add(new SimpleLabel("(Touch the big mud to unleash his mighty attack)", 40, 20, Color.BLACK));
    }

    private final Callback<ActionTarget> attackfinished = new Callback<ActionTarget>() {
        @Override
        public void on(ActionTarget element) {
            ((SharedSprite) element).animation().selectDefault().repeatForever().play();
        }
    };
}